package com.example.qsindusty.jmm_spline.LoginActivity;

import android.app.Activity;
import android.text.TextUtils;

import com.example.qsindusty.jmm_spline.api.ApiManager;
import com.example.qsindusty.jmm_spline.api.ApiManager;
import com.yixin.ystartlibrary.base.BasePresenter;
import com.yixin.ystartlibrary.base.IBaseView;
import com.yixin.ystartlibrary.utils.LogUtils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by Administration on 2018/3/5.
 */

public class LoginPresenter extends BasePresenter implements LoginContract.Perenter{
  LoginContract.View mView ;
    @Override
    public void bindView(IBaseView view) {
        mView = (LoginContract.View) view;
    }

    @Override
    public void unbindView() {
        dispose();
        mView= null;
    }


    @Override
    public void Login(final String username, final String userpassword, Activity activity) {
            mView.showLoading();
            // jmm MANAGER   DRIVER：司机、COMPANY：企业、MANAGER：管理员
        ApiManager.getInstence().getJmmApi().login(username,userpassword,"MANAGER")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LoginData jsonElement) {
                        if (TextUtils.equals(jsonElement.getStatus(),"0"))
                     mView.refresshDataSuccessful(jsonElement,username,userpassword);
                        else
                            mView.refreshDataFraier(jsonElement.getMessage());
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.d("onError:"+e.getMessage());
                        mView.closeLoading();
                        mView.refreshDataFraier(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    mView.closeLoading();
                    }
                });


    }


}
